package com.boe.csb.web.controller.base;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.alibaba.fastjson.JSON;
import com.boe.csb.core.common.exception.CSBErrorCode;
import com.boe.csb.core.common.exception.CSBException;
import com.boe.csb.core.common.utils.FtpUtil;
import com.boe.csb.core.common.utils.MyRandomUtils;
import com.boe.csb.core.service.user.BoeUserFtpService;
import com.boe.csb.web.controller.BaseController;
import com.boe.csb.web.model.ResultModel;

/**
 * Created by tianxiang.luo on 16/12/19.
 */
@RequestMapping(value = "/api/upload")
@RestController
public class UploadFileController extends BaseController {


    @Autowired
    private BoeUserFtpService boeUserFtpService;


    //上传图片
    @RequestMapping(value = "/pic", method = RequestMethod.POST)
    public ResultModel<Long> uploadPic(HttpSession session, @RequestParam CommonsMultipartFile serviceLogo) throws IOException {
        String userId = fetchUserId(session);
        String filePath = FtpUtil.getDatePath();
        if (serviceLogo != null) {
            String[] picArr = serviceLogo.getOriginalFilename().split("\\.");
            if (picArr.length >= 2) {
                String idCard = MyRandomUtils.randomUUID() + "." + picArr[picArr.length - 1];
                //上传身份证
                if (!FtpUtil.upload(filePath, idCard, serviceLogo.getInputStream())) {
                    throw new CSBException(CSBErrorCode.ACTIVE_FAIL, "");
                }
                Long sequenceId = boeUserFtpService.insertUserFtp(Long.valueOf(userId), filePath, idCard, null, null);
                
                return ResultModel.success(sequenceId);
            }
        }
        return null;
    }
}
